package com.kichina.parking.upstream;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kichina.parking.exception.extend.JsonParseException;
import com.kichina.parking.service.logic.bean.ParkingLotRecord;
import com.kichina.parking.service.logic.service.ParkingLotRecordService;
import com.kichina.parking.utils.JsonToEntityUtil;
import com.kichina.parking.utils.UUIDUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author Surging
 * @version 1.0
 * @ClassName MonthPayUpstreamHandler
 * @date 2022-09-14 20:03
 * @desc 车位变动后主动上传车位信息业务处理层
 */
@Component
@Slf4j
public class Parking3SpaceUpstreamHandler {

    private final ParkingLotRecordService parkingLotRecordService;

    public Parking3SpaceUpstreamHandler(ParkingLotRecordService parkingLotRecordService) {
        this.parkingLotRecordService = parkingLotRecordService;
    }

    /**
     * @param jsonObject 数据实体
     * @author Surging
     * @Description 车位变动后主动上传车位信息业务处理
     * @Return void
     * @date 20:53 2022-09-15
     **/
    public void recordParkingSpaceUpstream(JSONObject jsonObject) throws JsonParseException {
        DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        DateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd");
        ParkingLotRecord parkingLotRecord;
        try {
            parkingLotRecord = JsonToEntityUtil.jsonToEntity(jsonObject, ParkingLotRecord.class);
            if (Objects.nonNull(jsonObject.getString("time"))) {
                if(String.valueOf(jsonObject.getString("time")).length()<15){
                    //字符串格式两种 2022-10-15; 2022-09-16 15:54:00;
                    parkingLotRecord.setTimeDate(simpleDateFormat2.parse(String.valueOf(jsonObject.getString("time"))));
                }else {
                    parkingLotRecord.setTimeDate(simpleDateFormat.parse(String.valueOf(jsonObject.getString("time"))));
                }
            }
            LambdaQueryWrapper<ParkingLotRecord> eq = new QueryWrapper<ParkingLotRecord>().lambda()
                    .eq(ParkingLotRecord::getParkid, parkingLotRecord.getParkid());
            List<ParkingLotRecord> parkingLotRecords = parkingLotRecordService.list(eq);
            if (CollectionUtils.isNotEmpty(parkingLotRecords)){
                //更新
                parkingLotRecord.setUpdateTime(new Date());
                parkingLotRecordService.update(parkingLotRecord, eq);
            }else {
                //添加
                parkingLotRecord.setParkingLotRecordId(UUIDUtil.getUUID());
                parkingLotRecord.setUpdateTime(new Date());
                parkingLotRecord.setCreateTime(new Date());
                parkingLotRecordService.save(parkingLotRecord);
            }
        } catch (ParseException e) {
            log.error("数据格式不合法，解析失败");
            throw new JsonParseException("数据格式不合法，解析失败");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
